<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>权限管理</title>
</head>
<body>
<div th:fragment="main" id="auth">
<link rel="stylesheet" th:href="@{/css/roleGroup/auth.css}">
<link rel="stylesheet" th:href="@{/assets/jqGrid/css/jqgridReset.css}">
<div class="auth-header clearfix">
	<form class="navbar-form" onsubmit="return false;">
        <div class="groups form-group">
            <span class="find">
                <i class="fa fa-search"></i>
            </span> 
            <input type="text" class="form-control search" id="search" placeholder="请输入权限名称">
        </div>
        <input type="button" value="搜索" class="btn btn-primary nav-btn" id="searchUser">
    </form>
    <a href="#platform-roleGroup-addAuth" id="addAuth" class="addUser btn btn-primary nav-btn">新建权限</a>
</div>
<div id="authBox">
	<table id="authTable"></table>
    <div id="authPager"></div> 
</div>
<!--删除提示模态框  -->
<div id="deleteNotice" class="modal fade bs-example-modal-sm notice" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h3 class="modal-title" id="myModalLabel">操作提示</h3>
            </div>
            <div class="modal-body">
                确认删除此条数据吗？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="deleteData">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        	</div>
    	</div>
   	</div>
</div>
<script th:src="@{/assets/jqGrid/js/grid.locale-cn.js}"></script>
<script th:src="@{/assets/jqGrid/js/jquery.jqGrid.src.js}"></script>
<script th:inline="javascript">
var parentHeight,parentWidth;
function autodivheight(){
    parentHeight = $("#authBox").height() - 95;
    parentWidth = $("#authBox").width();
}
$(function () {
	autodivheight();
	function operation(cellvalue,options,rowObject){
		var str = "";
		if([[${#authorization.expression('hasAuthority(''modifyAuthority'')')}]]){
			str += "<input style='margin:5px 3px' class='btn btn-primary btn-xs modify' type='button' value='修改'>";
		}
		if([[${#authorization.expression('hasAuthority(''deleteAuthority'')')}]]){
			str += "<input style='margin:5px 3px' class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='.bs-example-modal-sm'>";
		}
        return str;
    }
	function filtType(cellvalue,options,rowObject){
		//,formatter: filtType
		switch(cellvalue){
			case 1:
				return "内置权限";
			case 2:
				return "自定义权限";
			default:
				return "未知类型";
		}
	}
	$("#authTable").jqGrid({
		url: contextPath + "/authority/queryAuthorityJqg",
		datatype: "json",
		height: parentHeight,
	    width: parentWidth,
		colNames:['权限ID','权限名称','描述','权限所属模块','类型','操作','','',''],
		colModel:[
			{name:'id',index:'id', hidden: true},
			{name:'name',index:'name'},
			{name:'description',index:'description'},
			{name:'authId',index:'authId'},
			{name:'type',index:'type'},
			{name:'operation',index:'operation', sortable: false,formatter: operation,align: "center"},
			{name:'systemId',index:'systemId', hidden: true},
			{name:'authorityContent',index:'authorityContent', hidden: true},
			{name:'moduleId',index:'moduleId', hidden: true}
		],
		sortname:'id',
		sortorder:'asc',
		viewrecords: true,
		multiselect: true,
		rowNum: 20,
		rowList:[5,10,20],
		pager: '#authPager'
	});
	$("#authTable").jqGrid('navGrid', '#authPager', {edit : false,add : false,del : false,search: false});
});
var timer = 0;
$(window).resize(function(){
	timer = setTimeout(function() {
		$("#authTable").setGridWidth($('#authBox').width());
		$("#authTable").setGridHeight($('#authBox').height() - 95);
		clearTimeout(timer);
    }, 260);
});　
// 侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click',function() {
    var time = 0;
    var set = setInterval(function (){
        $("#authTable").setGridWidth($('#authBox').width());　　
        $("#authTable").setGridHeight($('#authBox').height() - 95);
        time++;
        if(time >= 200){
            clearInterval(set);
        }
    },time);
});
//搜索
$("#searchUser").on('click',function () {
    $("#authTable").setGridParam({
         url : contextPath + "/authority/queryAuthorityJqg",
         postData : {
        	 queryCond:{
        		 pattern: $('#search').val()
        	 }
         },
        page: 1
    }).trigger("reloadGrid");
});
$('#search').on('keypress',function(event){  
    var theEvent = window.event || event;
    var code = theEvent.keyCode || theEvent.which;
    if(code == 13) {
        $("#authTable").setGridParam({
            url : contextPath + "/authority/queryAuthorityJqg",
            postData : {
                queryCond:{
                	pattern: $('#search').val()
                }
            },
            page: 1
        }).trigger("reloadGrid");
    }
});
//修改
var authData = null;
/* 修改 || 删除 */
$("#authTable").on('click','.modify,.del',function(){
	var id = $(this).parent().parent().attr('id');
	authData = $("#authTable").jqGrid('getRowData',id);
    delete authData.operation;
    if($(this).hasClass("modify")){
   		window.location.hash = "platform-roleGroup-modifyAuth";
    }else{
    	authData.authorities = JSON.parse(authData.authorities);
    }
});
$("#deleteData").on('click',function(){
	$.ajax({
        url:contextPath + '/authority/deleteAuthority',
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(authData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                $("#authTable").trigger("reloadGrid");
//                $("#authTable").setGridParam({
//                    url : contextPath + "/authority/queryAuthorityJqg",
//                    postData : {
//                        queryCond:{
//                            pattern: $('#search').val()
//                        }
//                    },
//                    page: 1
//                }).trigger("reloadGrid");
            }else{
                toastr.error("删除失败!",data.message);
            }
        },
        error:function(){
            toastr.error("删除失败!",data.message);
        }
    });
});
</script>
</div>
</body>
</html>